package com.example.kehoobackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.kehoobackend.domain.Answer;
import com.example.kehoobackend.domain.DayInfo;
import com.example.kehoobackend.domain.Question;
import com.example.kehoobackend.domain.User;
import com.example.kehoobackend.persistance.DayInfoMapper;
import com.example.kehoobackend.service.AnswerService;
import com.example.kehoobackend.service.DayInfoService;
import com.example.kehoobackend.service.QuestionService;
import com.example.kehoobackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service("dayInfoService")
public class DayInfoServiceImpl extends ServiceImpl<DayInfoMapper, DayInfo> implements DayInfoService {

    @Autowired
    UserService userService;

    @Autowired
    QuestionService questionService;

    @Autowired
    AnswerService answerService;


    @Override
    public void initialDate() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 生成当天与过去10天的日期字符串
        for (int i = 0; i <= 10; i++) {
            LocalDate pastDate = currentDate.minusDays(i);
            String dateString = pastDate.format(formatter);
            DayInfo dayInfo = new DayInfo();
            dayInfo.setDate(dateString);
            save(dayInfo);
        }
    }

    @Override
    public void initialDayInfo() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Answer> answerQueryWrapper = new QueryWrapper<>();

        List<DayInfo> dayInfoList = list();

        for(DayInfo dayInfo : dayInfoList){
            String date = dayInfo.getDate();
            userQueryWrapper.eq("DATE(join_time)",date);
            questionQueryWrapper.eq("DATE(create_time)",date);
            answerQueryWrapper.eq("DATE(create_time)",date);
            Integer new_users_count = (int)userService.count(userQueryWrapper);
            Integer new_questions_count = (int)questionService.count(questionQueryWrapper);
            Integer new_answers_count = (int)answerService.count(answerQueryWrapper);

            dayInfo.setNew_users_count(new_users_count);
            dayInfo.setNew_questions_count(new_questions_count);
            dayInfo.setNew_answers_count(new_answers_count);

            updateById(dayInfo);

            userQueryWrapper.clear();
            questionQueryWrapper.clear();
            answerQueryWrapper.clear();

        }
    }

    @Override
    public List<DayInfo> getRecentDayInfo() {

        List<String> recentDates = new ArrayList<>();
        List<DayInfo> recentDayInfo;

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 生成当天与过去6天的日期字符串
        for (int i = 0; i <= 6; i++) {
            LocalDate pastDate = currentDate.minusDays(i);
            String dateString = pastDate.format(formatter);
            recentDates.add(dateString);
        }
        recentDayInfo = listByIds(recentDates);
        for(DayInfo dayInfo : recentDayInfo){
            LocalDate date = LocalDate.parse(dayInfo.getDate());
            int month = date.getMonthValue();
            int day = date.getDayOfMonth();
            dayInfo.setMonthAndDay(month+"."+day);
        }

        return recentDayInfo;
    }

    @Override
    public void generateTodayInfo() {
        String currentDate = String.valueOf(LocalDate.now());
        if(getById(currentDate) == null){
            DayInfo dayInfo = new DayInfo();

            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
            QueryWrapper<Answer> answerQueryWrapper = new QueryWrapper<>();

            userQueryWrapper.eq("DATE(join_time)",currentDate);
            questionQueryWrapper.eq("DATE(create_time)",currentDate);
            answerQueryWrapper.eq("DATE(create_time)",currentDate);
            Integer new_users_count = (int)userService.count(userQueryWrapper);
            Integer new_questions_count = (int)questionService.count(questionQueryWrapper);
            Integer new_answers_count = (int)answerService.count(answerQueryWrapper);

            dayInfo.setDate(currentDate);
            dayInfo.setNew_users_count(new_users_count);
            dayInfo.setNew_questions_count(new_questions_count);
            dayInfo.setNew_answers_count(new_answers_count);

            save(dayInfo);
        }


    }


    @Override
    public void updateDayInfo(String currentDate){
        DayInfo dayInfo = new DayInfo();

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Answer> answerQueryWrapper = new QueryWrapper<>();

        userQueryWrapper.eq("DATE(join_time)",currentDate);
        questionQueryWrapper.eq("DATE(create_time)",currentDate);
        answerQueryWrapper.eq("DATE(create_time)",currentDate);
        Integer new_users_count = (int)userService.count(userQueryWrapper);
        Integer new_questions_count = (int)questionService.count(questionQueryWrapper);
        Integer new_answers_count = (int)answerService.count(answerQueryWrapper);

        dayInfo.setDate(currentDate);
        dayInfo.setNew_users_count(new_users_count);
        dayInfo.setNew_questions_count(new_questions_count);
        dayInfo.setNew_answers_count(new_answers_count);

        updateById(dayInfo);

    }


}
